<include file="header" />
<style type="text/css">
  body{background-color: #f1f1f1;}
</style>
<div id="content">
    <div id="weiba" data-title="发新帖" class="panel" data-selected="true"  data-menu="msgmenu">
        <header>
            <div id="header-buttons">
            	<a href="javascript:history.go(-1);">
                    <div id="back" class="header-menu-link" data-back="false">
                    </div>
                </a>
				<div class="header-menu-link" id="hmp-send">
                    <a class="sendBtn disable" onClick="doWeibaPost()" id="postbut" href="javascript:;">发布</a>
                </div>
            </div>
            <h1 class="hasback">
                发新帖
            </h1>
        </header>
      
      <div class="weibaPost">
      	<form method="post" action="{:addons_url('Weiba://Wap/doPost')}" id="doWeibaPost" onsubmit="doWeibaPost();return false;">
        <div class="navlist">
          <ul style="margin:0 0 10px">
             <li>
                <select style="color:#333" name="weiba_id" id="weiba_id" class="ts-select" >
                <option value="0">选择版块</option>
                <volist name="weibacate" id="vo">
                <php>if(empty($vo['weibalist']))continue;</php>
                <optgroup label="{$vo.name}" disabled>{$vo.name}</optgroup>
                    <volist name="vo.weibalist" id="wb">
                    <option <if condition="$weiba_id eq $wb['id']">selected="selected"</if> value="{$wb.id}">{$wb.weiba_name}</option>
                    </volist>
                </volist>
                </select>
             </li>
        	<!--
             <li>
                <select style="color:#333" name="tag_id" id="tag_id" class="ts-select">
                
                </select>
             </li>
             -->
         </ul>
      </div>
        
          <div class="pure-form send_box">
            <input type="text" id="title" name="title" class="ts-text" placeholder="填写标题，4-30个字符">
          </div>
          <div class="pure-form send_box">
          <textarea class="ts-textarea" name="content" id="post-content" rows="4" placeholder="填写文本，不少于10个字符"></textarea>
          </div>
          <div class="ts-footer">
              <!-- 表情 -->
              <!--<a class="icon facelistbutton ts-listen" data-listen="weibo-facelist-show"><i class="fa-btn fa-btn-smile"></i></a>
              -->
              <!-- 图片 -->
              <div class="upload_img_wrap">
                 <div class="controls">
                    <div class="upload_row muti_picture_row">
                        
                        <notempty name="data[$field['name']]">
                            <php>
                                $tempArr = explode(',',$data['img_ids']);
                                
                                for($i=0;$i<count($tempArr);$i++){
                                    echo '<div class="img_item"><em>X</em><input type="hidden" name="img_ids[]" value="'.$tempArr[$i].'"/><img src="'.get_cover_url($tempArr[$i]).'"/></div>';
                                }
                            </php>
                        </notempty>
                        <a id="upload_picture" class="img_item add_btn" href="javascript:;"><img width="100%" src="__IMG__/add.png"/></a>
                    </div>
                  </div>
              </div>
          </div>
          
        </form>
      </div>
    </div>
<script type="text/javascript" src="__STATIC__/webuploader-0.1.5/webuploader.min.js"></script>
<include file="footer" />
<script type="text/javascript">
h5Uploader('#upload_picture',2,'img_ids[]')
function h5Uploader(_this,num,name,callback){
    var count = num
    var uploader = WebUploader.create({
        // 设置文件上传域的name
        fileVal:'download',
        // 选完文件后，是否自动上传。
        auto: true,
        // swf文件路径
        swf: STATIC+"/webuploader-0.1.5/Uploader.swf",
        // 文件接收服务端。
        server: UPLOAD_PICTURE,
        // 选择文件的按钮。可选。
        // 内部根据当前运行是创建，可能是input元素，也可能是flash.
        pick:{
            id:_this,
            multiple:true
        } ,
        //验证文件总数量
        fileNumLimit:count,
        // 只允许选择图片文件。
        accept: {
            title: 'Images',
            extensions: 'gif,jpg,jpeg,png,bmp',
            mimeTypes: 'image/*'
        }
    });
    var uploadImgWidth = $('.muti_picture_row .img_item').width()-10;
    $('.webuploader-pick').height(uploadImgWidth).width(uploadImgWidth);
    // 文件上传成功，给item添加成功class, 用样式标记上传成功。
    uploader.on( 'uploadSuccess', function( file,res ) {
        var src = res.url || ROOT + res.path;
        var $li = $('<div id="'+file.id+'" class="img_item"><em>X</em><input type="hidden" name="'+name+'" value="'+res.id+'"/><img src="'+src+'"/></div>'),
            $img = $li.find('img');
        if(res.status){

            count--
            $li.insertBefore($(_this));
            
        } else {
                $.Dialog.fail(res.info)
            }

             $('.muti_picture_row .img_item').height(uploadImgWidth).width(uploadImgWidth);
             $('.muti_picture_row .img_item em').click(function(){
            $(this).parent().remove();
                uploader.removeFile( file );
                count++
            })
        // $list为容器jQuery实例
        console.log(count)
        $( '#'+file.id ).addClass('upload-state-done');
        if(typeof callback == "function" ){

            callback()
        }
    });
    // 文件上传过程中创建进度条实时显示。
    uploader.on( 'uploadProgress', function( file, percentage ) {
        $.Dialog.loading();

    });
    // 完成上传完了，成功或者失败，先删除进度条。
    uploader.on( 'uploadComplete', function( file ) {
       	$.Dialog.close();
    });
    // 文件上传失败，显示上传出错。
    uploader.on( 'uploadError', function( file ) {
         $.Dialog.fail('上传失败')
    });
    // 文件上传失败，显示上传出错。
    uploader.on( 'error', function( type ) {
         console.log(type)
    });

    
}
function doWeibaPost(){
	var weiba_id = $('#weiba_id').val();
	var tag_id = $('#tag_id').val();
	var title = $.trim($('#title').val());
	var content = $.trim($('#post-content').val());
	var imageIds = '';
	$('.img_item input').each(function(i, el){
		imageIds = $(el).val()+','+imageIds;
		
	});

	if(!weiba_id || weiba_id=='0'){
		$.ui.showMask("请选择版块",true);
	}else if(!title){
		$.ui.showMask("标题不能为空!",true);
		$('#title').focus();
	}else if(!content){
		$.ui.showMask("内容不能为空!",true);
		$('#post-content').focus();
	}else if(content.length<10){
		$.ui.showMask("内容不能少于10个字符",true);
		$('#post-content').focus();
	}else{
		$.post($('#doWeibaPost').attr('action'), {
			weiba_id: weiba_id,
			tag_id: tag_id,
			title   : title,
			content : content,
			imageIds: imageIds
		}, function(data){
			if(data.status){
				$('#title').val('');
				$('#post-content').val('');
				$('#ts-upload-img-box').empty();
				$.ui.showMask('发布成功', true);
				setTimeout(function(){
					window.location.href = data.url;
				},2000)
			}else{
				$.ui.showMask(typeof data=='string'?data:data.info,true);
			}
			
		},'JSON');
	}
}
/*
function getTags(tagId){
	var url = U('getTagsByAjax');
	tag_id=tagId?tagId:0;
	weiba_id = $('select[name="weiba_id"]').val();
	$.post(url,{tag_id:tag_id,weiba_id:weiba_id},function(data){
		$('select[name="tag_id"]').html(data);
	});
}
getTags({$tag_id});
*/
</script>